import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';

import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';

@Injectable()
export class UserService {
  private headers = new HttpHeaders().set('authorization', `Bearer ${localStorage.token}`);

  constructor(private http: HttpClient) { }

  register(user): Observable<any> {
    return this.http.post('/api/user', JSON.stringify(user), { headers: this.headers });
  }

  login(credentials): Observable<any> {
    return this.http.post('/api/web/v1/users/login', credentials);
  }

  getUser() {
    this.http.get<any[]>(`/api/web/v1/users/profile`, { headers: this.headers }).subscribe(data => data);
  }
}
